package com.nonintrusive.client.tabs;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.TabHost;

import com.nonintrusive.client.GlobalNonIntrusive;
import com.nonintrusive.client.send.R;
import com.nonintrusive.client.tabs.answerquestion.AnswerQuestionActivity;
import com.nonintrusive.client.tabs.askquestion.AskQuestionActivity;
import com.nonintrusive.client.tabs.history.HistoryActivity;
import com.nonintrusive.message.MessageNonIntrusive;

public class MainActivity extends TabActivity {

	private static final String TAG = "MainActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		// get me the id
		getMyClientId();

		intent = new Intent().setClass(this, HistoryActivity.class);
		spec = tabHost.newTabSpec("history")
				.setIndicator(getString(R.string.history)).setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AskQuestionActivity.class);
		spec = tabHost.newTabSpec("ask_question")
				.setIndicator(getString(R.string.ask_question))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AnswerQuestionActivity.class);
		spec = tabHost.newTabSpec("answer_question")
				.setIndicator(getString(R.string.answer_question))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AutoGenerateDB.class);
		spec = tabHost.newTabSpec("server_communication")
				.setIndicator("Server Comm").setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(3);
	}

	private void getMyClientId() {
		System.out.println("get client id");
		int myId = -1;
		SharedPreferences mPref = PreferenceManager
				.getDefaultSharedPreferences(this);
		final Editor mEditor = mPref.edit();

		// old client
		myId = mPref.getInt("clientID", -1);
		System.out.println("id meu " + myId);
		if (myId != -1)
		{
			synchronized (GlobalNonIntrusive.clientID) {
				GlobalNonIntrusive.clientID = myId;
			}
			return;
		}

		// new client
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Socket socket = new Socket(GlobalNonIntrusive.SERVER_IP,
							GlobalNonIntrusive.PORT);
					ObjectOutputStream oos = new ObjectOutputStream(socket
							.getOutputStream());
					oos.writeObject(new MessageNonIntrusive(
							MessageNonIntrusive.AUTH_TYPE));
					ObjectInputStream ois = new ObjectInputStream(socket
							.getInputStream());
					MessageNonIntrusive recvMessage = (MessageNonIntrusive) ois
							.readObject();

					Log.d(TAG, "answ to id message is |" + recvMessage.content
							+ "|");
					int id;
					id = Integer.parseInt(recvMessage.content);
					if ( id != -1) {
						mEditor.putInt("clientID", id);
						mEditor.commit();
						synchronized (GlobalNonIntrusive.clientID) {
							GlobalNonIntrusive.clientID = id;
						}
					}
					oos.close();
					ois.close();
					socket.close();
					System.out.println("sock closed");
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}

			}
		}).start();

	}
}
